<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class System_settings extends CI_Controller {
	public function index()
	{
                $data['title'] = "System Settings";
                
                $this->load->model("defaults");
                $defaults = $this->defaults->get();
                $available_times = $this->defaults->getTimes();
                
                $times = array();
                foreach($available_times->result() as $result){
                    $times[$result->available_time_id] = $result->available_time_time;
                }
                
                $data['js'] = array("system_settings.js");
                
                $data['data'] = array (
                    "title"=>"System Settings",
                    "times" => $times,
                    "defaults" => $defaults
                    );
                
                $data['view'] = "settings/view_system_settings";
                $this->load->view('view_template',$data);
	}
        
        public function save(){
            extract($this->ajax->post("serve_time","cutoff_time",
                    "mon","tue","wed","thu","fri","sat","sun",
                    "active"));
            
            if (!(isset($serve_time) && isset($cutoff_time) &&
                    isset($mon) && isset($tue) && isset($wed) && isset($thu) &&
                    isset($fri) && isset($sat) && isset($sun) && isset($active))){
                //One wasnt set, don't run script.
                echo "Insufficient data provided.";
                return;
            }
            
            $this->load->model('defaults');
            $return = $this->defaults->setDefaults($serve_time,$cutoff_time,
                    $mon,$tue,$wed,$thu,$fri,$sat,$sun,
                    $active);
            
            if ($return){
                $this->session->set_flashdata('message', "System settings updated.");
                echo "good";
            }
            else{
                $this->session->set_flashdata('message', "Server error. Contact an administrator if this persists.");
                echo "fail";
            }
        }
}

